package com.hyt.it.ogt.pj.data.service.pt.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.pj.data.mapper.pt.ExaminationCategoryMapper;
import com.hyt.it.ogt.pj.data.model.entity.pt.ExaminationCategory;
import com.hyt.it.ogt.pj.data.model.vo.pt.ExaminationCategoryDTO;
import com.hyt.it.ogt.pj.data.model.vo.pt.MultiTree;
import com.hyt.it.ogt.pj.data.service.pt.IExaminationCategoryService;
import com.hyt.it.ogt.pj.data.utils.BuildTree;
import com.hyt.it.ogt.pj.data.utils.DateTimeUtil;

/**
 * @author zhang_xiang
 * @version 1.0
 * @date 2021/11/30 14:12 考试分类的实现类
 */
@Service
public class ExaminationCategoryServiceImpl extends BaseServiceImpl<ExaminationCategoryMapper, ExaminationCategory> implements IExaminationCategoryService {

	@Resource
	ExaminationCategoryMapper categoryMapper;

	@DS("pt_read")
	@Override
	public MultiTree<ExaminationCategoryDTO> getTreelist() {
		// 首先查询所有的分类数据
		List<ExaminationCategory> categoryList = categoryMapper.selectGetList();
		List<MultiTree<ExaminationCategoryDTO>> trees = getTreesList(categoryList);
		return BuildTree.buildName(trees);
	}

	private List<MultiTree<ExaminationCategoryDTO>> getTreesList(List<ExaminationCategory> categoryList) {
		List<MultiTree<ExaminationCategoryDTO>> trees = new ArrayList<>();
		for (ExaminationCategory cateGoryBean : categoryList) {
			String parentId = cateGoryBean.getExaminationParentId();
			MultiTree<ExaminationCategoryDTO> tree = new MultiTree<>();
			tree.setId(cateGoryBean.getId());
			tree.setExaminationId(cateGoryBean.getExaminationId());
			tree.setCreateDate(DateTimeUtil.asDate(cateGoryBean.getCreateDate()));
			tree.setName(cateGoryBean.getExaminationName());
			tree.setCategorylevel(cateGoryBean.getExaminationLevel());
			trees.add(tree);
			for (ExaminationCategory value : categoryList) {
				if (value.getExaminationId().equals(parentId)) {
					tree.setParentName(value.getExaminationName());
				}
				tree.setParentId(parentId == null ? null : parentId);
			}
		}
		return trees;
	}

}
